package ph.gov.marikina.mcsmtms.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import ph.gov.marikina.mcsmtms.constants.GlobalConstants.Roads;
import ph.gov.marikina.mcsmtms.constants.GlobalConstants.SituationTypes;
import ph.gov.marikina.mcsmtms.constants.GlobalConstants.TrafficConditionTypes;
import ph.gov.marikina.mcsmtms.domain.SituationReport;
import ph.gov.marikina.mcsmtms.domain.SituationSegment;

// TODO: Auto-generated Javadoc
/**
 * The Class Utils.
 */
public class Utils {
	
	/** The Constant dateFormatString. */
	public final static String dateFormatString = "MM/dd/yyyy hh:mm:ss aaa";
	//public final static String ganttDateFormatString = "yyyy-MM-dd hh:mm:ss aaa";
	public final static String ganttDateFormatString = "dd-MM-yyyy hh:mm:ss aaa";
	
	/**
	 * Format default date.
	 *
	 * @param date the date
	 * @return the string
	 */
	public static String formatDefaultDate(Date date) {
		if(date == null) return "";
		return (new SimpleDateFormat(dateFormatString)).format(date);
	}
	
	/**
	 * Format date.
	 *
	 * @param date the date
	 * @param formatString the format string
	 * @return the string
	 */
	public static String formatDate(Date date, String formatString) {
		if(date == null) return "";
		return (new SimpleDateFormat(formatString)).format(date);
	}
	
	public static Date parseDate(String dateTimeString) {
		//return (new SimpleDateFormat(formatString)).format(date);
		if(dateTimeString.split("/").length < 3) 
			return null;
		try {
			String dateString = dateTimeString.split(" ")[0];
			int year = Integer.parseInt(dateString.split("/")[2]);
			int month = Integer.parseInt(dateString.split("/")[0]) - 1;
			int day = Integer.parseInt(dateString.split("/")[1]);
			
			if(dateTimeString.split(" ").length >= 2) {
				String timeString = dateTimeString.split(" ")[1];
				int hour = Integer.parseInt(timeString.split(":")[0]);
				int min = Integer.parseInt(timeString.split(":")[1]);
				int sec = Integer.parseInt(timeString.split(":")[2]);
				
				Calendar dateCalendar = new GregorianCalendar(year, month, day, hour, min, sec);
				return dateCalendar.getTime();
			} else {
				Calendar dateCalendar = new GregorianCalendar(year, month, day);
				return dateCalendar.getTime();
			} 
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}
	
	public static String generateAlertMessage(SituationReport situationReport) {
		if(situationReport == null) return "";
		
		String alertMessage = "";
		
		String situationDetails = "";
		
		try {
			for(SituationSegment segment : situationReport.getSituationSegmentList()) {
				situationDetails += SituationTypes.getDescriptionByCode(segment.getSituation()) + 
									"(" + TrafficConditionTypes.getDescriptionByCode(segment.getTrafficCondition())  + "), ";
			}
			if((situationDetails.length() - 2) >= 0)
				situationDetails = situationDetails.substring(0, situationDetails.length() - 2);
			
			alertMessage += situationDetails + " reported at " + 
								Roads.getDescriptionByCode(situationReport.getRoad()) + " " +
								situationReport.getLastUpdateDateTimeString();
		} catch (Exception ex) {
			ex.printStackTrace();
			alertMessage = "";
		}
		
		return alertMessage;
	}
	
	public static Date getLastTimeDate(Date date) {
		Calendar cal = new GregorianCalendar();
		
		cal.setTime(date);
		cal.set(cal.HOUR_OF_DAY, 23);
		cal.set(cal.MINUTE, 59);
		cal.set(cal.SECOND, 59);
		
		return cal.getTime();
	}
	
	public static Date getFirstTimeDate(Date date) {
		Calendar cal = new GregorianCalendar();
		
		cal.setTime(date);
		cal.set(cal.HOUR_OF_DAY, 0);
		cal.set(cal.MINUTE, 0);
		cal.set(cal.SECOND, 0);
		
		return cal.getTime();
	}
	
	public static Date convertToDefaultSystemDate(Date date, Date systemDate) {
		GregorianCalendar dateGc = new GregorianCalendar();
		dateGc.setTime(date);
		GregorianCalendar systemDateGc = new GregorianCalendar();
		systemDateGc.setTime(systemDate);
		dateGc.set(Calendar.YEAR, systemDateGc.get(Calendar.YEAR));
		dateGc.set(Calendar.MONTH, systemDateGc.get(Calendar.MONTH));
		dateGc.set(Calendar.DATE, systemDateGc.get(Calendar.DATE));
		
		dateGc.set(Calendar.HOUR_OF_DAY, 10);
		
		return dateGc.getTime();
	}
}
